#delimit;

set more off;

capture log close;

local t1="specify here the folder path where the data are stored";
local t2="specify here the folder path where you want the output data to be stored";



log using `t2'mergewave2_rel231_ext, replace;

drop _all;

set mem 1G;


use `t1'sharew2_rel2-3-1_ch1;

merge  mergeid using `t1'sharew2_rel2-3-1_dn1;
tab _merge;
drop _merge;
sort  mergeid;

merge  mergeid using `t1'sharew2_rel2-3-1_ep1;
tab _merge;
drop _merge;
sort  mergeid;
merge  mergeid using `t1'sharew2_rel2-3-1_hh1;
tab _merge;
drop _merge;
sort  mergeid;
merge  mergeid using `t1'sharew2_rel2-3-1_ft1;
tab _merge;
drop _merge;
sort  mergeid;
merge  mergeid using `t1'sharew2_rel2-3-1_ph1;
tab _merge;
drop _merge;
sort  mergeid;
merge  mergeid using `t1'sharew2_rel2-3-1_sp1;
tab _merge;
drop _merge;
sort  mergeid;
merge  mergeid using `t1'sharew2_rel2-3-1_gv_weights1;
tab _merge;
drop _merge;
sort mergeid;

merge  mergeid using `t1'sharew2_rel2-3-1_gv_isced1;
tab _merge;
drop _merge;
sort mergeid;

merge  mergeid using `t1'sharew2_rel2-3-1_hc1;
tab _merge;
drop _merge;
sort mergeid;


sort hhid2 cvid;
merge hhid2 cvid using `t1'sharew2_rel2-3-1_cv_r1;
tab _merge;
keep if _merge==3;
drop _merge;
sort  mergeid;

sort hhid2;
merge hhid2 using `t1'sharew2_rel2-3-1_cv_h1;
tab _merge;
keep if _merge==3;
drop _merge;
sort  mergeid;

merge mergeid using `t1'sharew2_rel2-3-1_imputations1;
tab _merge;
keep if implicat==1;
keep if _merge==3;*Elimino a Israel y tambi�n a los no elegibles o no entrevistados;
drop _merge;
sort mergeid;
drop if country==28 | country==29;


*We recover info on gender, age and health of parents who are interviewed;
en casa y elegibles para entrevista;
gen relrpers1=relrpers if relrpers==5 | relrpers==6;
replace relrpers1=0 if (relrpers==5 | relrpers==6) & hhid2==hhid2[_n-1] & (relrpers[_n-1]==5 | relrpers[_n-1]==6)  ;
gen relrpers1b=relrpers if relrpers1==0 & (relrpers==5 | relrpers==6);
egen relrpers2=sum(relrpers1), by (hhid2);
sort hhid2 mergeid;
egen relrpers2b=sum(relrpers1b), by (hhid2);
sort hhid2 mergeid;

gen pgender=gender if (relrpers==5 | relrpers==6);  
replace pgender=0 if (relrpers==5 | relrpers==6) & hhid2==hhid2[_n-1] & (relrpers[_n-1]==5 | relrpers[_n-1]==6)  ;
gen pgender2=gender if pgender==0 & (relrpers==5 | relrpers==6);
egen ppgender=sum(pgender), by (hhid2);
sort hhid2 mergeid;
egen ppgender2=sum(pgender2), by (hhid2);
sort hhid2 mergeid;

gen age=int_year_w2-dn003;
gen page=age if (relrpers==5 | relrpers==6);  
replace page=0 if (relrpers==5 | relrpers==6) & hhid2==hhid2[_n-1] & (relrpers[_n-1]==5 | relrpers[_n-1]==6)  ;
gen page2=age if page==0 & (relrpers==5 | relrpers==6);
egen ppage=sum(page), by (hhid2);
sort hhid2 mergeid;
egen ppage2=sum(page2), by (hhid2);
sort hhid2 mergeid;
gen phealth=ph003 if (relrpers==5 | relrpers==6);
replace phealth=0 if (relrpers==5 | relrpers==6) & hhid2==hhid2[_n-1] & (relrpers[_n-1]==5 | relrpers[_n-1]==6)  ;
gen phealth2=ph003 if phealth==0 & (relrpers==5 | relrpers==6);
egen pphealth=sum(phealth), by (hhid2);
sort hhid2 mergeid;
egen pphealth2=sum(phealth2), by (hhid2);

sort hhid2 mergeid;
sort mergeid;
drop language;
save `t2'mergewave2_rel231_ext.dta, replace;




log close;
